package ferror

import (
	"fmt"
	"io"
)

// 应用错误
type AppError struct {
	Msg      string `json:"msg"`
	Code     string `json:"code"`
	SubError error  `json:"-"`
}

func (a AppError) Error() string {
	/*	if a.SubError != nil {
		appError, ok := a.SubError.(AppError)
		if ok {
			return appError.Error()
		}
	}*/
	if a.Code != "" {
		return a.Code + ":" + a.Msg
	}
	return a.Msg
}

func (w AppError) Format(s fmt.State, verb rune) {
	switch verb {
	case 'v':
		if s.Flag('+') {
			if w.Code != "" {
				io.WriteString(s, w.Code+":"+w.Msg+"\n")
			} else {
				io.WriteString(s, w.Msg+"\n")
			}
			if w.SubError != nil {
				fmt.Fprintf(s, "%+v\n", w.SubError)

			}
			return
		}
		fallthrough
	case 's', 'q':
		io.WriteString(s, w.Error())
	}
}
